<?php

namespace DebugBar\DataFormatter\VarDumper;

use Symfony\Component\VarDumper\Cloner\Cursor;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\DumperInterface;
use Symfony\Component\VarDumper\Cloner\Stub;


class SeekingData extends Data
{
        
    
    private $position = 0;
    private $key = 0;

    
    public function seek($key)
    {
        $thisData = $this->getRawData();
        $item = $thisData[$this->position][$this->key];

        if (!$item instanceof Stub || !$item->position) {
            return;
        }
        $keys = array($key);

        switch ($item->type) {
            case Stub::TYPE_OBJECT:
                $keys[] = "\0+\0".$key;
                $keys[] = "\0*\0".$key;
                $keys[] = "\0~\0".$key;
                $keys[] = "\0$item->class\0$key";
            case Stub::TYPE_ARRAY:
            case Stub::TYPE_RESOURCE:
                break;
            default:
                return;
        }

        $data = null;
        $children = $thisData[$item->position];

        foreach ($keys as $key) {
            if (isset($children[$key]) || array_key_exists($key, $children)) {
                $data = clone $this;
                $data->key = $key;
                $data->position = $item->position;
                break;
            }
        }

        return $data;
    }

    
    public function dump(DumperInterface $dumper)
    {
                $refs = array(0);
        $class = new \ReflectionClass($this);
        $dumpItem = $class->getMethod('dumpItem');
        $dumpItem->setAccessible(true);
        $data = $this->getRawData();
        $args = array($dumper, new Cursor(), &$refs, $data[$this->position][$this->key]);
        $dumpItem->invokeArgs($this, $args);
    }
}
